﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using mscook.core;
using mscook.core.domain;
using mscook.core.repositories;
using mscook.mvc.core;
using mscook.core.services;
using System.Web.Security;
using mscook.mvc.Core;
using System.Linq.Expressions;

namespace mscook.mvc.Areas.weblogin.Controllers
{
    [UserAuthorizeAttribute(Roles = "admin")]
    public class PresentController : BaseController
    {
        //
        // GET: /weblogin/Present/
        private ILoginUserService iLoginUserService;
        private IPresentRepository iPresentRepository;
        private IImageSettingFactory iImageSettingFactory;
        private readonly string SectionName = "Magazine";


        public PresentController(ILoginUserService iLoginUserService, IPresentRepository iPresentRepository, IImageSettingFactory iImageSettingFactory)
            : base(iLoginUserService)
        {
            this.iLoginUserService = iLoginUserService;
            this.iPresentRepository = iPresentRepository;
            this.iImageSettingFactory = iImageSettingFactory;
        }
        public ActionResult Index(string nowPageNum, string per = "",string name="")
        {
            Expression<Func<Present, bool>> select_expression = o => (string.IsNullOrEmpty(name) || o.Name.Contains(name));

            PageBreakHelper pageHelper = new PageBreakHelper();
            pageHelper.Init(nowPageNum, per);
            long total = 0;
            IList<Present> loginUserList = iPresentRepository.SelectPageList(pageHelper.Start, pageHelper.PerValue, out total, select_expression);
            pageHelper.SetViewDataKeys(ViewData, ViewBag, total);

            InitNavBar();
            ViewData["name"] = name;
            ViewData["nowPageNum"] = nowPageNum;
            ViewData["per"] = per;
            return View(loginUserList);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Add()
        {
            InitNavBar();
            return View();
        }



        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Add(Present present)
        {
            if (ModelState.IsValid)
            {
                if (SaveUploadImage(present))
                {
                    present.ResourceBonusSetting.Bonus = -Math.Abs(present.ResourceBonusSetting.Bonus);
                    iPresentRepository.SaveOrUpdate(present);

                    return RedirectToIndex();
                }
            }
            return View();
        }
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Edit(string id)
        {
            Present present = iPresentRepository.Get(id);
            if (present != null)
            {


                InitNavBar();
                return View(present);
            }
            //If result is null, redirect to index
            return RedirectToIndex();
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(string id, Present newPresent)
        {
            Present present = iPresentRepository.Get(id);

            if (ModelState.IsValid)
            {

                UpdateModel(present);
                if (SaveUploadImage(present))
                {
                    iPresentRepository.SaveOrUpdate(present);
                    return RedirectToIndex();
                }
            }
            return View();
        }

        private bool SaveUploadImage(Present present)
        {
            if (Request.Files.Count > 0)
            {
                HttpPostedFileBase file = Request.Files.Get(0);
                if (file.ContentLength != 0)
                    present.ImageName = iImageSettingFactory.CreateBatchSizedImage(SectionName, file.FileName, file.InputStream);
                return true;
            }
            return false;
        }

    }
}
